package com.itheima.consumer.listeners;

//import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2024-07-07
 * Time:16:36
 */
@Component
public class ListenAMQP {

    @RabbitListener(queues = "demo1.queue")
    public void listenDemo1(String msg){
        System.out.println("接收到消息：" +msg);

    }

    @RabbitListener(queues = "work.queue")
    public void listenwork1(String msg) throws InterruptedException {
        System.out.println("接收到消息：" + msg);
        Thread.sleep(20);
    }
    @RabbitListener(queues = "work.queue")
    public void listenwork2(String msg) throws InterruptedException {
        System.err.println("接收到消息：" + msg);
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenfan1(String msg) throws InterruptedException {
        System.out.println("接收到消息：" + msg);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenfan2(String msg) throws InterruptedException {
        System.err.println("接收到消息：" + msg);
    }

    @RabbitListener(queues = "dir.queue1")
    public void listendir1(String msg) throws InterruptedException {
        System.out.println("接收到" + msg);
    }
    @RabbitListener(queues = "dir.queue2")
    public void listendir2(String msg) throws InterruptedException {
        System.err.println("接收到" + msg);
    }

    @RabbitListener(queues = "topic.queue1")
    public void listentopic1(String msg) throws InterruptedException {
        System.out.println("接收到" + msg);
    }
    @RabbitListener(queues = "topic.queue2")
    public void listentopic2(String msg) throws InterruptedException {
        System.err.println("接收到" + msg);
    }

//    @Bean
//    public Queue lazyQueue(){
//        return (Queue) QueueBuilder.durable("lazy.queue").lazy().build();
//    }

    @RabbitListener(queuesToDeclare = @Queue(
            name = "lazy.queue",
            durable = "true",
            arguments = @Argument(name = "x-queue-mode",value = "lazy")
    ))
    public void listen(String msg) throws InterruptedException {
        System.err.println("接收到" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg){
//        System.out.println("消费者1接收到topic.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "demoqueue.direct"),
            exchange = @Exchange(name = "demo.direct", type = ExchangeTypes.DIRECT),
            key = "news"
    ))
    public void listenTopicQueue2(String msg){
        System.out.println("消费者2接收到topic.queue2的消息：【" + msg + "】");
        throw new RuntimeException("故意的");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "error.queue"),
            exchange = @Exchange(name = "error.direct", type = ExchangeTypes.DIRECT),
            key = "error"
    ))
    public void listen_demo(String msg){
        System.out.println("接收到error内容");
    }

    @Bean
    public MessageRecoverer republishRecover(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate, "error.direct", "error");
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "delay.direct", delayed = "true"),
            key = "delay"
    ))
    public void listenDelayMessage(String msg){
        System.out.println("接收到delay.queue的延迟消息：" +  msg);
    }


}
